<template>
    <div style="width: 520px">
        <el-input
                style="margin-bottom: 8px"
                placeholder="输入部门名称进行搜索。。。"
                prefix-icon="el-icon-search"
                v-model="filterText">
        </el-input>

        <el-tree
                :expand-on-click-node="false"
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                ref="tree">
            <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%" slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <span>
                  <el-button
                          type="primary"
                          size="mini" class="depButton"
                          @click="() => showAddDepView(data)">
                    添加部门
                  </el-button>
                  <el-button
                          type="danger"
                          size="mini" class="depButton"
                          @click="() => deleteDep(data)">
                    删除部门
                  </el-button>
                </span>
                </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <div>
                    <el-tag style="margin-right: 8px">上级部门</el-tag><span>{{pname}}</span>
                </div>
                <div>
                    <el-tag style="margin-right: 8px">部门名称</el-tag><el-input style="width: 200px" v-model="dep.name" placeholder="请输入部门名称"></el-input>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddDep">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "DepManager",
        data(){
            return{
                dep:{
                    name:'',
                    parentid:-1,
                },
                pname:'',
                filterText:'',
                deps:[],
                defaultProps:{
                    children: 'children',
                    label: 'name'
                },
                dialogVisible:false,
            }
        },
        methods:{
            showAddDepView(data){
                this.pname = data.name;
                this.dep.parentid = data.id;
                this.dialogVisible=true;
            },
            addDep2Deps(deps,dep){
                for (let i=0;i<deps.length;i++){
                    let d = deps[i];
                    if (d.id == dep.parentid){
                        d.children=d.children.concat(dep);
                        return
                    } else {
                        this.addDep2Deps(d.children,dep);
                    }
                }
            },
            doAddDep(){
                this.postRequest("/system/basic/department/",this.dep).then(resp=>{
                    if (resp){
                        this.addDep2Deps(this.deps,resp.obj);
                    }
                    this.pname = '';
                    this.dep.parentid = -1;
                    this.dep.name='';
                    this.dialogVisible = false;
                })
            },
            removeDepFromDeps(deps,id){
                for (let i=0;i<deps.length;i++){
                    let d = deps[i];
                    if (d.id == id){
                        deps.splice(i,1);//从i开始删，删一个；
                        return;
                    } else{
                        this.removeDepFromDeps(d.children,id);
                    }
                }
            },
            deleteDep(data){
                if (data.isparent && data.children !=null&&data.children.length>0){
                    this.$message.error("父部门不能直接删除")
                } else {
                    this.$confirm('此操作将永久删除【'+data.name+'】, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/system/basic/department/"+data.id).then(resp=>{
                            if (resp){
                                this.removeDepFromDeps(this.deps,data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            initDeps(){
                this.getRequest("/system/basic/department/").then(resp=>{
                    if (resp){
                        this.deps = resp;
                    }
                })
            },
        },
        mounted() {
            this.initDeps();
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
    }
</script>

<style>
    .el-tree-node__label {
        font-size: 18px;
    }
    .depButton {
        padding: 2px;
    }
</style>